package br.com.teamware.ontrack.expenses;

import java.util.HashSet;

import com.domainlanguage.money.Money;
import com.domainlanguage.time.TimeInterval;
import com.domainlanguage.time.TimePoint;

public class ExpenseSet extends HashSet<Expense> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6073948598832792316L;
	
	public Money getTotalAmount() {
		Money totalAmount = Money.dollars(0);
		for (Expense expense : this) {
			totalAmount = totalAmount.plus(expense.getCost());
		}
		return totalAmount;
	}

	public TimeInterval getPeriod() {
		TimePoint from=null;
		TimePoint to=null;
		for (Expense expense : this) {
			if(from == null)
				from = expense.getDue();
			if(to == null )
				to = expense.getDue();
			
			if(from.isAfter(expense.getDue()))
				from = expense.getDue();
			
			if(to.isBefore(expense.getDue()))
				to = expense.getDue();
		}
		return TimeInterval.closed(from, to);
	}

	public ExpenseSet getNext(Money dollars) {
		ExpenseSet nexts = new ExpenseSet();
		Money totalAmount = Money.dollars(0);
		
		for (Expense expense : this) {
			totalAmount = totalAmount.plus(expense.getCost());
			if(totalAmount.isGreaterThan(dollars))
				return nexts;
			nexts.add(expense);
		}
		return nexts;
	}

}
